/* IMF_IFS.do (STATA)
	Construct IMF IFS data.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using IMF_IFS, replace;

/* Define local and global variables */

local directory = "../../Data/IMF/IFS";

do global;


/* Step 1: Construct national accounts data */

/* Load data */

u Country_Code Indicator_Code Attribute _`=$year_min-1'-_`=$year_max+1'
	if regexm(Indicator_Code,"^(NCP_R_XDC|NCP_XDC|NGDP_D_IX|NGDP_R_XDC|NGDP_XDC)$")
	& Attribute=="Value"
	using `directory'/IFS_06-26-2023, clear;

drop Attribute _*Q* _*M*;

/* Fix variables */

destring Country_Code _*, replace;

/* Reshape in long format */

reshape long _, i(Country_Code Indicator_Code) j(year);
reshape wide _, i(Country_Code year) j(Indicator_Code) string;

/* Rename variables */

foreach var in NCP_R_XDC NCP_XDC NGDP_D_IX NGDP_R_XDC NGDP_XDC {;
	rename _`var' `var';
};

/* Convert to LCU billion */

foreach var of varlist NCP_R_XDC NCP_XDC NGDP_R_XDC NGDP_XDC {;
	replace `var' = `var'/1e9;
};

/* Save data */

tempfile IMF_IFS;
save `IMF_IFS';


/* Step 2: Construct financial data */

/* Load data */

u Country_Code Indicator_Code Attribute _`=$year_min-1'-_`=$year_max+1'M12
	if regexm(Indicator_Code,"^(ENDE_XDC_USD_RATE|FIGB_PA|FITB_PA|FPOLM_PA|PCPI_IX)$")
	& Attribute=="Value"
	using `directory'/IFS_06-26-2023, clear;

keep Country_Code Indicator_Code _*Q* _*M*;

/* Fix variables */

destring Country_Code _*, replace;

/* Collapse to one observation per country and indicator */

collapse (firstnm) _*, by(Country_Code Indicator_Code) fast;

/* Fill missing month-end with quarter-end */

forval y = `=$year_min-1'/$year_max {;
	forval q = 1/4 {;
		replace _`y'M`=3*`q'' = _`y'Q`q' if missing(_`y'M`=3*`q'');
	};
};

drop _*Q*;

/* Keep year-end data */

keep Country_Code Indicator_Code _*M12;

/* Reshape in long format */

reshape long _, i(Country_Code Indicator_Code) j(date) string;
reshape wide _, i(Country_Code date) j(Indicator_Code) string;

/* Merge country code */

merge m:1 Country_Code using Countries,
	keepusing(country Yeuro)
	keep(master match);

keep if _merge==3 | Country_Code==163;	/* 163 = euro area */

drop _merge;

/* Rename variables */

foreach var in ENDE_XDC_USD_RATE FIGB_PA FITB_PA FPOLM_PA PCPI_IX {;
	rename _`var' `var';
};

rename ENDE_XDC_USD_RATE fx;
rename PCPI_IX cpi;

/* Convert exchange rates to US$ per LCU */

replace fx = 1/fx;

/* Convert interest rates to decimal */

foreach var of varlist FIGB_PA FITB_PA FPOLM_PA {;
	replace `var' = `var'/100;
};

/* Construct variables */

gen int year = real(substr(date,1,4));

drop date;

/* Construct dummy for euro area and Denmark */

gen byte Ieuro = Country_Code==163 | year>=Yeuro | country=="DNK";

drop Yeuro;

/* Common exchange rate and policy rate in the euro area */

sort year Ieuro country;

foreach var of varlist fx FPOLM_PA {;
	by year Ieuro: replace `var' = `var'[1] if Ieuro & Country_Code[1]==163;
};

drop if Country_Code==163;

drop Ieuro;

/* Construct dummy for USD countries */

gen byte Iusd = inlist(country,"USA","HKG");

/* Common exchange rate and policy rate for USD countries */

sort year Iusd country;

foreach var of varlist fx FPOLM_PA {;
	by year Iusd: replace `var' = `var'[_N] if Iusd & country[_N]=="USA";
};

drop Iusd;


/* Step 3: Merge national accounts and financial data */

merge 1:1 year Country_Code using `IMF_IFS',
	nogen keep(master match);

drop Country_Code;

/* Sample criteria */

keep if inrange(year,$year_min-1,$year_max+1);

/* Label variables */

order year country;

label var year			"Year";

label var fx			"Exchange rate (US$ per LCU)";
label var FIGB_PA		"Government bond yield";
label var FITB_PA		"Treasury bill rate";
label var FPOLM_PA		"Central bank policy rate";
label var cpi			"Consumer price index";

label var NCP_R_XDC		"Household consumption expenditure, Real index (LCU billion)";
label var NCP_XDC		"Household consumption expenditure, Nominal (LCU billion)";
label var NGDP_D_IX		"GDP, Deflator";
label var NGDP_R_XDC	"GDP, Real index (LCU billion)";
label var NGDP_XDC		"GDP, Nominal (LCU billion)";

/* Save data */

sort year country;

save IMF_IFS, replace;

log close;
